using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Org.BouncyCastle.Bcpg.OpenPgp;
namespace Pbdms.Common.SharedVariables
{
    public class SecretKeyControl
    {
        static ReaderWriterLock rwSecret= new ReaderWriterLock();
        static ReaderWriterLock rwPrivate = new ReaderWriterLock();
        private PgpPrivateKey privateKey;
        private PgpSecretKey secretKey;
        public PgpSecretKey readSecretKey()
        {
            do{
                try{
                    rwSecret.AcquireReaderLock(-1);
                }
                catch(System.ApplicationException exception)
                {
                }
            }while(!rwSecret.IsReaderLockHeld);
            try{
                return secretKey;
            }finally
            {
                rwSecret.ReleaseReaderLock();
            }
        }
        public void setSecretKey(PgpSecretKey secretKey)
        {
            do{
                try{
                    rwSecret.AcquireWriterLock(-1);
                }
                catch(System.ApplicationException exception)
                {
                }
            }while(!rwSecret.IsWriterLockHeld);
            try
            {
                this.secretKey = secretKey;
            }finally
            {
                rwSecret.ReleaseWriterLock();
            }
        }
        public PgpPrivateKey readPrivateKey()
        {
            do
            {
                try
                {
                    rwPrivate.AcquireReaderLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwPrivate.IsReaderLockHeld);
            try
            {
                return privateKey;
            }
            finally
            {
                rwPrivate.ReleaseReaderLock();
            }
        }
        public void setPrivateKey(PgpPrivateKey privateKey)
        {
            do
            {
                try
                {
                    rwPrivate.AcquireWriterLock(-1);
                }
                catch (System.ApplicationException exception)
                {
                }
            } while (!rwPrivate.IsWriterLockHeld);
            try
            {
                this.privateKey= privateKey;
            }
            finally
            {
                rwPrivate.ReleaseWriterLock();
            }
        }
    }
}
